package com.example.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.noear.redisx.RedisClient;
import org.noear.redisx.plus.RedisBucket;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.redisson.api.RedissonClient;

/**
 * 学生web控制器
 *
 * @author raos
 * @date 2024-12-14
 */
@Slf4j
@Mapping("/test")
@Controller
public class TestController {

    @Inject("redissonClient1")
    private RedissonClient redissonClient1;

    @Inject("redissonClient2")
    private RedissonClient redissonClient2;

    @Inject
    private RedisClient redisClient;

    @Mapping("/add1")
    public Object add1(String key, Long value) {
        redissonClient1.getAtomicLong(key + "_1").set(value);
        return redissonClient1.getAtomicLong(key).get();
    }

    @Mapping("/get1")
    public Object get1(String key) {
        return redissonClient1.getAtomicLong(key + "_1").incrementAndGet();
    }

    @Mapping("/add2")
    public Object add2(String key, Long value) {
        redissonClient2.getAtomicLong(key + "_2").set(value);
        return redissonClient2.getAtomicLong(key).get();
    }

    @Mapping("/get2")
    public Object get2(String key) {
        return redissonClient1.getAtomicLong(key + "_2").addAndGet(2);
    }

    @Mapping("/set")
    public Object set(String key, String value) {
        RedisBucket bucket = redisClient.getBucket();
        bucket.store(key, value);
        return "ok->" + bucket.get(key);
    }

}
